//
//  RHPicChartView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/7/26.
//

#import "RHPicChartView.h"
@interface RHPicChartView()

@property(nonatomic,assign)NSInteger onLine;

@property(nonatomic,assign)NSInteger offLine;

@end
@implementation RHPicChartView
- (instancetype)init
{
    if([super init]){
        self.backgroundColor = [UIColor whiteColor];
        _onLine = 0;
        _offLine = 0;
    }
    return self;
}

-(void)setOnlienCount:(NSInteger)onLine offlineCount:(NSInteger)offLine
{
    _onLine = onLine;
    _offLine = offLine;
    [self setNeedsDisplay];
}


-(void)drawRect:(CGRect)rect
{
    CGFloat onLinePercent = 0;
    CGFloat offLinePercent = 0;
    if(_onLine + _offLine != 0){
        onLinePercent = _onLine*1.0f/(_onLine+_offLine);
        offLinePercent = 1.0 - onLinePercent;
    }
    if(self.onLine+self.offLine==0){
        offLinePercent = 1;
    }

   //白色背景 离线
    CGFloat offStartAngle = 0;
    CGFloat offEndAngle = M_PI * 2 * offLinePercent;
    CGFloat radius = MIN(self.width, self.height)/2;
    CGPoint center_off = CGPointMake(radius, radius);
    CGFloat offline_radius = radius - 10;
    UIBezierPath *offPath = [UIBezierPath bezierPathWithArcCenter:center_off radius:offline_radius startAngle:offStartAngle endAngle:offEndAngle clockwise:YES];
    [[UIColor colorWithHexString:@"DEE3E8"]setFill];
    [offPath addLineToPoint:center_off];
    [offPath moveToPoint:center_off];
    [offPath closePath];
    [offPath fill];
    
    
    //绿色 在线
    CGFloat onEndAngle = offEndAngle;
    if(onLinePercent == 1){//在线100%
        onEndAngle = M_PI*2;
    }
    CGFloat centerX = center_off.x + 5;
    CGFloat centerY = center_off.y - 5;
//    CGPoint center_on = CGPointMake(centerX, centerY);
    CGFloat online_radius = radius - 8;
    if(onLinePercent == offLinePercent){
        online_radius = radius - 10;
    }
    CGPoint center_on = [self getCenterFromAngle:onEndAngle offLineCenter:center_off];
    UIBezierPath *onPath = [UIBezierPath bezierPathWithArcCenter:center_on radius:online_radius startAngle:offStartAngle endAngle:onEndAngle clockwise:NO];
    [[UIColor colorWithHexString:@"56C271"]setFill];
    [onPath addLineToPoint:center_on];
    if(_onLine != 0){
        [onPath fill];
    }

}
//根据在线的角度值计算出中心点的位置
-(CGPoint)getCenterFromAngle:(CGFloat)angle  offLineCenter:(CGPoint)center
{
    //注意:苹果角度的0度是在X轴的右半边,按顺时针旋转到180度后就是-180度,再-90度,再回到0度
    CGFloat adjustAngle = angle;
    
//    CGFloat one = 180 / M_PI;//一度
//    NSLog(@"---->sin30:%f",sinf(30*one));
//    NSLog(@"---->cosin30:%f\n",cosf(30*one));
//    
//    NSLog(@"---->sin90:%f",sinf(-90*one));
//    NSLog(@"---->cos90:%f\n",cosf(-90*one));
//    
//    NSLog(@"---->sin120:%f",sinf(120*one));
//    NSLog(@"---->cos120:%f\n",cosf(120*one));
//    
//    NSLog(@"---->sin180:%f",sinf(M_PI));
//    NSLog(@"---->cos180:%f",cosf(M_PI));
    
    if(angle>M_PI) {
        //上半部分
        adjustAngle = -(M_PI - (M_PI * 2 - angle/2));
        CGFloat x = cosf(adjustAngle)*6 + center.x;
        CGFloat y = -sinf(adjustAngle)*6 + center.y;
        
        return CGPointMake(x, y);
    }else{
        //下半部分
        adjustAngle =  angle / 2;
        CGFloat x = -cosf(adjustAngle)*6 + center.x;
        CGFloat y = -sinf(adjustAngle)*6 + center.y;
        return CGPointMake(x, y);
    }
    
    
    return center;
}
@end
